<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class SystemAccount extends BaseModel
{
    protected $table = 'system_account';

    const CREATED_AT = 'add_time';
    const UPDATED_AT = 'upd_time';

    protected $dateFormat = 'U';

    protected $fillable = [
        'name',
        'value',
    ];

    protected $data = [];

    /**
     * 保存所有已设置的数据
     * @param array $options
     */
    public function apply()
    {
        $saved = true;
        foreach ($this->data as $name=>$object){
            $saved = $saved && $object->save();
        }
        return $saved;
    }

    /**
     * 获取系统钱包余额
     * wallet_balance
     */
    public function getWalletBalanceAttribute()
    {
        if (empty($this->data['wallet_balance'])){
            $this->data['wallet_balance'] = $this->firstOrCreate(['name'=>'WalletBalance'],['value'=>0]);
        }
        return (double)$this->data['wallet_balance']->value;
    }

    /**
     * 设置系统钱包余额
     * wallet_balance
     */
    public function setWalletBalanceAttribute($value)
    {
        $this->wallet_balance;
        $this->data['wallet_balance']->value = doubleval($value);
    }

}
